<?php
/*
 * 1626cms 會員登錄注冊
 * ============================================================================
 * 版權所有: 1626網絡，并保留所有權利。
 * 網站地址: http://www.1626.com；
 * ----------------------------------------------------------------------------
 * 這不是一個自由軟件！您只能在不用于商業目的的前提下對程序代碼進行修改和
 * 使用；不允許對程序代碼以任何形式任何目的的再發布。
 * ============================================================================
 */
define('IN_QISHI', true);
$page_select="user";
require_once('./include/common.inc.php');
require_once(QISHI_ROOT_PATH.'include/mysql.class.php');
require_once(QISHI_ROOT_PATH.'include/fun_user.php');
$db = new mysql($dbhost,$dbuser,$dbpass,$dbname);
unset($dbhost,$dbuser,$dbpass,$dbname);
$smarty->caching = false;
$act = !empty($_REQUEST['act']) ? trim($_REQUEST['act']) : 'login';
if(!$_SESSION['uid'] && !$_SESSION['username'] && !$_SESSION['utype'] && $_COOKIE['Qishi']['username'] && $_COOKIE['Qishi']['password'])
{
	if(check_cookie($_COOKIE['Qishi']['username'],$_COOKIE['Qishi']['password']))
	{
		update_user_info($_COOKIE['Qishi']['username'],false);
	}
	else
	{
		setcookie('Qishi[username]',"", time() - 3600,$cookiepath, $cookiedomain);
		setcookie('Qishi[password]',"", time() - 3600,$cookiepath, $cookiedomain);
	}
}
if($act == 'logout'){
	unset($_SESSION['uid']);
	unset($_SESSION['username']);
	unset($_SESSION['utype']);
	setcookie("Qishi[username]","",time() - 3600,$cookiepath, $cookiedomain);
	setcookie("Qishi[password]","",time() - 3600,$cookiepath, $cookiedomain);
	header("Location: ".$_CFG['site_dir']."");
	exit();
}
if ($_SESSION['username'] and $_SESSION['utype'])
{
	header("Location:".user_jump($_SESSION['utype']));
}
$act=='login'? ($smarty->assign('title','會員登錄 - '.$_CFG['site_name'])).($smarty->assign('error',$_GET['error'])).($smarty->display('login.htm')):'';
($act=='reg1'||$act=='reg2')&&$_CFG['closereg']=='1'?showmsg("網站暫停會員注冊，請稍后再次嘗試！",1):'';
//$act=='reg1'?($smarty->assign('title','會員注冊 - '.$_CFG['site_name'])).($smarty->display('reg1.htm')):'';
//$act=='reg2' && $_GET['member_type']=='' ? showmsg("請選擇注冊類型！",1):'';
//$act=='reg2' && $_GET['member_type']?($smarty->assign('title','會員注冊 - '.$_CFG['site_name'])).($smarty->assign('member_type',$_GET['member_type'])).($smarty->display('reg2.htm')):'';
$act=='reg1' ||$act=='reg2' ?($smarty->assign('title','會員注冊 - '.$_CFG['site_name'])).($smarty->display('reg2.htm')):'';
if ($act=='do_reg')
{
	require_once(QISHI_ROOT_PATH.'include/fun_user_company.php');
	$username = !empty($_POST['username']) ? trim($_POST['username']):showmsg('請給自己起個用戶名！',1);
	$password = !empty($_POST['password']) ? trim($_POST['password']):showmsg('請給自己設置一個密碼！',1);
	($password<>trim($_POST['password2']))? showmsg('兩次輸入的密碼不相同！',1):'';
	$member_type = !empty($_POST['member_type']) ? trim($_POST['member_type']):showmsg('請選擇會員類型！',1);
	$email = !empty($_POST['email']) ? trim($_POST['email']):showmsg('請輸入你常用的電子郵箱！',1);
	if (user_register($username,$password,$member_type,$email,$timestamp,$online_ip))
	{
		$login_url=user_login($username,$password);
		$mailconfig=get_cache('mailconfig');
		if ($mailconfig['set_reg']=="1")
		{
			asyn_sendmail($_CFG['site_domain'].$_CFG['site_dir']."asyn_mail.php?uid=".$_SESSION['uid']."&key=".asyn_userkey($_SESSION['uid'])."&sendemail=".$email."&sendusername=".$username."&sendpassword=".$password."&act=reg");
		}
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location.href=\"".$login_url."\";</script>";
	}
	else
	{
		showmsg('注冊失敗！',1);
	}
}
if ($act=='do_login')
{
	$username = !empty($_POST['username']) ? trim($_POST['username']):showmsg('請輸入賬戶名(email地址或者用戶名)！',1);
	$username=='email地址或者用戶名'?showmsg('請輸入賬戶名(email地址或者用戶名)！',1):'';
	$password = !empty($_POST['password']) ? trim($_POST['password']):showmsg('密碼不能為空！',1);
	header("Location:".user_login($username,$password));
}
//找回密碼第1步
if ($act=='get_pass_index')
{
	$smarty->assign('step',"1");
	$smarty->assign('title','找回密碼 - '.$_CFG['site_name']);
	$smarty->display('get_pass.htm');
}
//找回密碼第2步
if ($act=='get_pass_subun')
{
	$postusername=trim($_POST['username'])?trim($_POST['username']):showmsg('請輸入用戶名！',1);
	if (get_user_inusername($postusername))
	{
		$smarty->assign('step',"2");
		$smarty->assign('postusername',$postusername);
		$smarty->assign('title','找回密碼 - '.$_CFG['site_name']);
		$smarty->display('get_pass.htm');
	}
	else
	{
		$smarty->assign('step',"1");
		$smarty->assign('checkuser',"error");
		$smarty->assign('title','找回密碼 - '.$_CFG['site_name']);
		$smarty->display('get_pass.htm');
	}
}
//找回密碼第3步
if ($act=='getpass_subemail')
{
	$postusername=trim($_POST['postusername'])?trim($_POST['postusername']):showmsg('用戶名丟失！',1);
	$userinfo=get_user_inusername($postusername);
	if ($userinfo && $userinfo['email']=="".$_POST['email']."")
	{
		$mailconfig=get_cache('mailconfig');
		require_once(QISHI_ROOT_PATH.'PHPMailer/class.phpmailer.php');
		$arr['username']=$userinfo['username'];
		$arr['password']=rand(100000,999999);
		if ($arr['username']!=edit_password($arr,false)) showmsg('密碼修改出錯！',1);
		if (smtp_mail($userinfo['email'],"找回密碼","您的新密碼為：".$arr['password']))
		{
			$smarty->assign('step',"3");
			$smarty->assign('email',$email);
			$smarty->assign('title','找回密碼 - '.$_CFG['site_name']);
			$smarty->display('get_pass.htm');
		}
		else
		{
			$smarty->assign('step',"4");
			$smarty->assign('title','找回密碼 - '.$_CFG['site_name']);
			$smarty->display('get_pass.htm');
		}
	}
	else
	{
		$smarty->assign('step',"2");
		$smarty->assign('postusername',$postusername);
		$smarty->assign('checkuser',"error");
		$smarty->assign('title','找回密碼 - '.$_CFG['site_name']);
		$smarty->display('get_pass.htm');
	}
}
?>
